package com.newbingo.action;

import java.util.Date;
import java.util.List;
import com.googlecode.jsonplugin.annotations.JSON;
import com.newbingo.dto.AgentDto;
import com.newbingo.dto.BetSettingDto;

import com.newbingo.entity.Agent;
import com.newbingo.entity.BetSetting;
import com.newbingo.entity.ContactInfo;

import com.newbingo.service.imple.IAgentLogService;
import com.newbingo.service.imple.IAgentService;
import com.newbingo.service.imple.IGameService;
import com.newbingo.util.Pager;
import com.newbingo.util.GameTree;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;

/**
 * 代理Action
 * @author ChansonYan && MingliangTang
 */
public class AgentAction extends ActionSupport {
	//页面数据显示类
	private Pager pager = null;
    private BetSettingDto betSetting;
    //属性注入
	private IAgentService agentService = null;
	private IGameService gameService=null;
	private IAgentLogService agentLogService=null;
	//页面操作传入对象
	private Agent agent;
	private BetSetting bet;
	
   //根据id获取单条投注格记录的 参数传值
	private Integer agentId;
	private Integer gamePlayId;
	private Integer cellId;	
	/*
	 * 分页条件
	 */
	private int start;
	private int limit=10;
	private Integer agentType;
	//树形的生成JSON
	private String treeJson;
	// 判断方法执行是否正确
	private Boolean success = false;
	
	
	//修改密码传参
	private String oldPwd;
	private String newPwd;
	

	


	/********************************操作方法*****************************/
	/*
	 * 会员点击设置游戏，页面加载树形节点，显示游戏，玩法
	 * 点击玩法节点，加载该代理所属玩法的集合
	 */
	public String createTreeJson(){
		treeJson=GameTree.getGamePlaysJson(gameService.findAll());
		success=true;
		System.out.println(treeJson);
		return "betSettingtree";
	}
	
	/*
	 * 调用BetSettingDto中的方法
	 * 获取BetSettingDto转换之后的数据
	 */
	public String findBetSetting(){
		HttpSession session=ServletActionContext.getRequest().getSession();
		AgentDto agentDto=(AgentDto)session.getAttribute("user");
		this.setPager(agentService.findBetSetting(agentDto.getUno(), gamePlayId));
	    success=true;
		return SUCCESS;
	}
	
	/*
	 * 根据登录的代理的ID和下级类型查询下级的代理或股东,并进行分页显示
	 * atentType=0表示查询下级代理, agentType=1表示查询股东
	 */
	public String findSubAgents(){
		HttpSession session=ServletActionContext.getRequest().getSession();
		AgentDto agentDto=(AgentDto)session.getAttribute("user");
		pager=agentService.findSubAgents(start, limit, agentDto.getUno(), agentType);
		success=true;
		return SUCCESS;
	}

	
	/*
	 * 根据代理的ID查看操作记录
	 */
	public String findOperationLog(){
		pager=agentLogService.findByPage(start, limit, agentId);
		success=true;
		return "log";
	}
	
	/*
	 * 新增下级代理或股东方法
	 * 页面隐藏域中传入agent.parentAgent.uno(session中获取),和agentType
	 * atentType=0表新增下级代理, agentType=1表示新增股东
	 */
	public String create(){
		HttpSession session=ServletActionContext.getRequest().getSession();
		AgentDto ag=(AgentDto)session.getAttribute("user");
		System.out.println("Action："+ag.getUno());
		if (agent!=null) {
			agent.setStatus("正常");
			agent.setAgentType(agentType);
			agent.setUno(ag.getUno());
			agent.setBaseAmount(0);
			agent.setBalance(0);
			agent.setRegTime(new Date());
			agentService.save(agent);
			success=true;
		}else{
			success=false;
		}
//		return "create";

		return SUCCESS;

	}
	
	/*
	 * 删除
	 */
	public String delete(){
		Agent gent=agentService.findById(agent.getUno());
		agentService.delete(gent);
		success=true;
		return SUCCESS;
//		return "delete";
	}
	
	/**
	 * 
	 * @return
	 */
	public String changePwd(){
		HttpSession session=ServletActionContext.getRequest().getSession();
		AgentDto ag=(AgentDto)session.getAttribute("user");
		Agent agent=agentService.findById(ag.getUno());
		if (oldPwd!=null && newPwd!=null) {
			if (oldPwd.equals(agent.getUpwd())) {
				agent.setUpwd(newPwd);
				agentService.merge(agent);
				success=true;
			}else {
				success=false;
			}
		}else {
			success=false;
		}
		return "changePass";
	}
	
	
	/*
	 * 修改代理或股东信息
	 */
	public String updateAgent(){
		Agent gent=agentService.findById(agent.getUno());
		if (agent==null) {
			success=false;
		}else {
			gent.setTrueName(agent.getTrueName());
			ContactInfo info=new ContactInfo();
			info.setTel(agent.getContactInfo().getTel());
			info.setEmail(agent.getContactInfo().getEmail());
			gent.setContactInfo(info);
			agentService.merge(gent);
			success=true;
		}
		return SUCCESS;
	}
	
	/*
	 * 修改代理或股东状态,由上级代理设置
	 */
	public String updateStatus(){
		Agent gent=agentService.findById(agent.getUno());
		if (agent==null) {
			success=false;
		}else {
			gent.setStatus(agent.getStatus());
			agentService.merge(gent);
			success=true;
		}

		return SUCCESS;
//		return "change";

	}
	
	
	/*
	 * BetSetting
	 * 修改投注限额
	 */
	public String updateBetSetting(){
       agentService.mergeSetting(bet);
       success=true;
       return SUCCESS;
//       return "betSet";
	}
	

	/*
	 * 根据agentId，gamePlayId，cellId 获取每一条betSetting记录到betSettingEdit中 显示修改
	 */
//	public String getBetSettingDtoObjectById(){
//		HttpSession session=ServletActionContext.getRequest().getSession();
//		AgentDto agentDto=(AgentDto)session.getAttribute("user");
////		System.out.println("Action中输入的传值："+this.getAgentId()+""+this.getGamePlayId()+""+this.getCellId());
//		this.setBetSetting(agentService.getBetSettingDtoObjectById(agentDto.getUno(), gamePlayId, cellId));
//		success=true;
//		return SUCCESS;
////		return "dto";
//	}

	
//	@JSON(serialize=false)
	public Pager getPager() {
		return pager;
	}
	public void setPager(Pager pager) {
		this.pager = pager;
	}

	@JSON(serialize = false)
	public IAgentService getAgentService() {
		return agentService;
	}
	public void setAgentService(IAgentService agentService) {
		this.agentService = agentService;
	}
	
	@JSON(serialize=false)
	public Agent getAgent() {
		return agent;
	}
	public void setAgent(Agent agent) {
		this.agent = agent;
	}
	

	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	

	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	

	public Boolean getSuccess() {
		return success;
	}
	public void setSuccess(Boolean success) {
		this.success = success;
	}
	

	public BetSetting getBet() {
		return bet;
	}
	public void setBet(BetSetting bet) {
		this.bet = bet;
	}
	
	@JSON(serialize=false)
	public IGameService getGameService() {
		return gameService;
	}
	public void setGameService(IGameService gameService) {
		this.gameService = gameService;
	}
	

	public String getTreeJson() {
		return treeJson;
	}
	public void setTreeJson(String treeJson) {
		this.treeJson = treeJson;
	}
	

	public Integer getAgentId() {
		return agentId;
	}
	public void setAgentId(Integer agentId) {
	
		this.agentId = agentId;
	}
	

	public Integer getGamePlayId() {
		return gamePlayId;
	}
	public void setGamePlayId(Integer gamePlayId) {
		this.gamePlayId = gamePlayId;
	}
	

	public BetSettingDto getBetSetting() {
		return betSetting;
	}
	public void setBetSetting(BetSettingDto betSetting) {
		this.betSetting = betSetting;
	}
	

	public Integer getCellId() {
		return cellId;
	}
	public void setCellId(Integer cellId) {
		this.cellId = cellId;
	}
	
	public Integer getAgentType() {
		return agentType;
	}
	public void setAgentType(Integer agentType) {
		this.agentType = agentType;
	}
	
	@JSON(serialize=false)
	public IAgentLogService getAgentLogService() {
		return agentLogService;
	}
	public void setAgentLogService(IAgentLogService agentLogService) {
		this.agentLogService = agentLogService;
	}

	public String getOldPwd() {
		return oldPwd;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}


	
}
